/**
*基础供电归属 js
*/ 

var	powerSupplyDS;
var powerSupplyFormPanel;
function doPowSupDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JPowerSupplyWeb.deletePowerSupplyBO(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					powerSupplyDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//powerSupplyFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var powersupplyType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'powersupply', type: "string"},
							   {name: 'pstelephone', type: "string"}, 
							   {name: 'contact', type: "string"}, 
							   {name: 'telephone', type: "string"},  
							   {name: 'refreshtime',  type: 'date'},
							   {name: 'memo',  type: 'string'}
						 ]);
	var powersupplyCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "归属供电所",  sortable: true, dataIndex: 'powersupply',align:'left'},	
				{header: "供电所联系电话",  sortable: true, dataIndex: 'pstelephone',align:'left'},	
				{header: "联系人",  sortable: true, dataIndex: 'contact',align:'left'},	   
				{header: "联系人电话",  sortable: true, dataIndex: 'telephone',align:'left'},	    				
				//{header: "更新日期",  width: 150,sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d h:M:s'),dataIndex: 'refreshtime',align:'left'},	 
				{header: "删除", dataIndex: 'id',renderer: showDelete}					
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doPowSupDelete(\"" + id  +   "\")'>";  
	}
		
	powerSupplyDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JPowerSupplyWeb.getPowerSupplyList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, powersupplyType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var powersupplyGrid = new Ext.grid.GridPanel({			 	
			ds: powerSupplyDS,
			cm: powersupplyCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            powerSupplyFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: powerSupplyDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});


	powerSupplyDS.on('load', function() {
		powersupplyGrid.getSelectionModel().selectFirstRow(); 
	});		
	
	powerSupplyFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
		labelWidth : 130,  
		height: 190,
		minSize: 200,
		maxSize: 200,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			 {
				columnWidth:.33,
				layout: 'form',  
				labelWidth:100,	 
				labelAlign:'right',	
				items: [
					{
						xtype:'hidden',
						fieldLabel: '流水号',
						width:150,
						name: 'id'
					}, {
						xtype:'textfield',
						readOnly: true,
						fieldLabel: '基站编号',
						width:150,
						name: 'btscode'
					}, {
						xtype:'textfield',
						fieldLabel: '基站名称',
						width:150,
						readOnly: true,
						name: 'btsname'
					}
					,{
						xtype:'textarea',
						fieldLabel: '备注',
						width:150,
						name: 'memo'
					}
					]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelWidth:110,	 
				labelAlign:'right',	
				items: [
					{
						xtype:'textfield',
						fieldLabel: '供电归属的供电所',
						width:150,
						name: 'powersupply'
					}, {
						xtype:'textfield',
						fieldLabel: '供电所联系电话',
						width:150,
						name: 'pstelephone'
					}
					]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelWidth:130,	 
				labelAlign:'right',	
				items: [ 
					{
						xtype:'textfield',
						fieldLabel: '基站外聘发电人员姓名',
						width:150,
						name: 'contact'
					},{
						xtype:'textfield',
						fieldLabel: '外聘发电人员联系电话',
						width:150,
						name: 'telephone'
					}]
			 }
			],
		buttons :[
			{
				text : ' 保存',
				handler : savePowerSup
			},{
				text : ' 新建/取消',
				handler : function(){
					powerSupplyFormPanel.getForm().reset(); 
					powerSupplyFormPanel.getForm().loadRecord(recordForAll);	
				}
			}
			]	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'powersupplyTab',
		items    : [
			powersupplyGrid,
			powerSupplyFormPanel
		]
	});
	
	
	function savePowerSup()
	{
		if (!powerSupplyFormPanel.getForm().isValid())
			return;	
			
		var form = powerSupplyFormPanel.getForm().getValues(false);	
		PowerSupplyBO = {};
		PowerSupplyBO.id = form.id;
		PowerSupplyBO.btscode = form.btscode;
		PowerSupplyBO.btsname= form.btsname;
		PowerSupplyBO.powersupply = form.powersupply;
		PowerSupplyBO.pstelephone = form.pstelephone;
		PowerSupplyBO.contact = form.contact;
		PowerSupplyBO.telephone = form.telephone;
		PowerSupplyBO.memo = form.memo;
		 	   			
		JPowerSupplyWeb.savePowerSupplyBO(PowerSupplyBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				loadDate(powerSupplyDS,powerSupplyFormPanel);	 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
		
	loadDate(powerSupplyDS,powerSupplyFormPanel);
	
})
